home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5091 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.4 KB

  1. Path: news.clark.net!not-for-mail
  2. From: gusty@clark.net (Harlan Messinger)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with float/double data types
  5. Date: 31 Jan 1996 21:08:57 GMT
  6. Organization: Clark Internet Services, Inc., Ellicott City, MD USA
  7. Message-ID: <4eolp9$726@clarknet.clark.net>
  8. References: <96030.105940RWL380B@MAINE.MAINE.EDU>
  9. NNTP-Posting-Host: explorer.clark.net
  10. Mime-Version: 1.0
  11. Content-Type: TEXT/PLAIN; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
  14.  
  15. RWL380B@MAINE.MAINE.EDU wrote:
  16. : C++ Experts,
  17. : If I declare X, Y, Z and Zstd as float:
  18. :     float X=0.0,Y=0.0,Z=0.0,Zstd=0.0;
  19. : Then the output looks correct but the digits after the decimal points
  20. : in X are incorrect (original first line X = -2082085.85000).  The
  21. : first value of Y is incorrect, but the next values are correct!
  22. :  X-Coordinate     Y-Coordinate       Z-Est     Z StdDev    n
  23. : -2082085.87500    -2127847.00000    23.12121    1.77931    8
  24. : -2082085.87500    -2047847.12500    23.13800    1.73910    8
  25. : -2082085.87500    -1967847.12500    23.15039    1.69751    8
  26. : -2082085.87500    -1887847.12500    23.15789    1.65532    8
  27. : -2082085.87500    -1807847.12500    23.15990    1.61336    8
  28. : -2082085.87500    -1727847.12500    32.19845    1.55000    8
  29. : -2082085.87500    -1647847.12500    38.46202    1.49013    8
  30. : -2082085.87500    -1567847.12500    32.17017    1.45502    8
  31. : -2082085.87500    -1487847.12500    39.51803    1.40543    8
  32. : -2082085.87500    -1407847.12500    47.27689    1.37395    8
  33. : If I declare X,Y,Z and Zstd to be double then the output is
  34. : correct.
  35. :  X-Coordinate     Y-Coordinate      Z-Est      Z StdDev    n
  36. : -2082085.85000    -2127847.12500    23.12121    1.77931    8
  37. : -2082085.85000    -2047847.12500    23.13800    1.73910    8
  38. : -2082085.85000    -1967847.12500    23.15039    1.69751    8
  39. : -2082085.85000    -1887847.12500    23.15789    1.65532    8
  40. : -2082085.85000    -1807847.12500    23.15990    1.61336    8
  41. : -2082085.85000    -1727847.12500    32.19845    1.55000    8
  42. : -2082085.85000    -1647847.12500    38.46202    1.49013    8
  43. : -2082085.85000    -1567847.12500    32.17017    1.45502    8
  44. : -2082085.85000    -1487847.12500    39.51803    1.40543    8
  45. : -2082085.85000    -1407847.12500    47.27689    1.37395    8
  46. : Coming from Fortran, this should not be happening.
  47.  
  48. Why not? The difference between real and double precision in Fortran is 
  49. about the same as between float and double in C++.
  50.  
  51.   Borland's
  52. : documentation suggests that negative floats and doubles don't
  53. : exist.  
  54.  
  55. You are misreading it somehow.
  56.  
  57. However, their web page has a technical document
  58. : describing how floating points are represented and indicate
  59. : that negative floats and doubles are OK.  I am puzzled
  60. : by the behavior I describe above.  And yes, I need access to
  61. : the X, Y and Z values as numbers for certain statistics I
  62. : need to include so I can't just read them as strings.
  63. : Any help is greatly Appreciated!
  64.  
  65. Floating point numbers are stored, at least on PCs, in binary scientific 
  66. notation. The sign of the number is stored in one bit. The absolute value 
  67. of the number is then expressed uniquely in the form s * 2^e (s times 2 
  68. to the e power), where s >= 0.5 but s < 1, and e is an integer that can 
  69. be positive, negative or zero. The values s (significand) and e (exponent) 
  70. are stored in some set of bits.
  71.  
  72. On a PC, a 32-bit float uses one bit for the sign, 8 bits for the 
  73. exponent and the remaining 23 bits for the significand. The significand 
  74. is expressed as a binary fractional expansion; the first digit is chopped 
  75. off (it is always 1, since s is at least 1/2 but less than 1, so to save 
  76. space and allow greater precision the 1 is assumed instead of being saved 
  77. explicitly); and the next 23 binary places are stored. The exponent e is 
  78. stored as a signed integer.
  79.  
  80. The number -2127847.12500 can be expressed in binary scientific notation 
  81. as (-).1[00000011101111110011100]1000... times 2 to the 22nd power. The bits 
  82. between brackets are the 23 bits that would be saved in a float. What 
  83. remains after the right bracket is the rounding error, which is 
  84. approximately 1 in the 25th position after the point--that is, 2 to the 
  85. negative 25th power times 2 to  the 22nd power = 2 to the negative 3rd 
  86. power = 1/8 = 0.125, exactly the amount by which your printed output was off.
  87.  
  88. The number -2047847.12500 in binary scientific notation is 
  89. (-).1[11110011111101100111001] EXACTLY. This value can be specified fully 
  90. in the 24 binary places provided (23 stored plus one imputed), so no 
  91. rounding error occurs.
  92.  
  93. I haven't checked your other numbers individually, but this gives you the 
  94. general idea.
  95.  
  96. How many digits of precision does one get from 24 binary places (bits)? 
  97. The rightmost bit in the significand represents 2^(-24), which is about 6 
  98. times 10^(-8). If all the bits in the signficand are 1s, then the 
  99. significand represents 1. Therefore, the precision of the signficand is 
  100. equal to 6 parts per 100,000,000, allowing about seven signficant figures 
  101. in decimal representation.
  102.  
  103. The 8-bit exponent can vary from -127 to 128, so the magnitude of floats 
  104. can range from 0.5 * 2^(-127) to 1.0 * 2^128. This is equivalent to a 
  105. range from 10^(-38) to 10^38 in decimal terms.
  106.  
  107. Doubles are stored in 64 bits on a PC: one bit for sign, 11 for exponent, 
  108. and 52 for significand. The 52 bits allow for about 15 signficant digits. 
  109. The 11 bits in the exponent allow for orders of magnitude from 10^(-308) 
  110. to 10^308.
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.